$(document).ready(function () {
    var uid = 0;
    var page = 0;
    $.getJSON('fontend.php?class=auth&action=uid', function (data) {
        uid = data.uid;
    });
    $('#btn-book-qsearch').click(function () {
        if ($('#form-bsearch').val() === '') {
            $('#form-bsearch').focus();
        } else {
            $.getJSON('fontend.php', {class: 'userlib', action: 'query', condition: $('#form-bsearch').val()}, function (data) {
                var tb = '';
                $.each(data, function (idx, it) {
                    tb += '<tr id=trx"'+it.bid+'"><th class="td-bid">' + it.bid + '</th><td class="td-bkname">' + it.bname + '</td><td calss="td-bkauthor">' + it.bkauthor + '</td><td class="td-bkpub">' + it.pubname + '</td><td class="td-isbn">' + it.bkisbn + '</td><td><button type="button" class="btn btn-primary btn-xs sdetail" data-bid="' + it.bid + '">Modify</button></td><td class="td-price">' + it.bkprice + '</td><td class="td-opt"><button type="button" class="btn btn-danger btn-xs bkremove" data-bid="' + it.bid + '">Delete</button></td></tr>';
                });
                if (tb === '') {
                    tb = '<tr><td colspan="8"> Not Found </td></tr>';
                }
                $('#tblist').html(tb);
            });
        }
    });

    $('#bnlistbook').click(function () {
        var st = page * 20 + 1;
        var ed = (page + 1) * 20 + 1;
        $.getJSON('admin.php', {class: 'bookadmin', action: 'list', start: st, end: ed}, function (data) {
            var tb = '';
            $.each(data, function (idx, it) {
                tb += '<tr id="trx'+it.bid+'"><th class="td-bid">' + it.bid + '</th><td class="td-bkname">' + it.bname + '</td><td calss="td-bkauthor">' + it.bkauthor + '</td><td class="td-bkpub">' + it.pubname + '</td><td class="td-isbn">' + it.bkisbn + '</td><td><button type="button" class="btn btn-primary btn-xs sdetail" data-bid="' + it.bid + '">Modify</button></td><td class="td-price">' + it.bkprice + '</td><td class="td-opt"><button type="button" class="btn btn-danger btn-xs bkremove" data-bid="' + it.bid + '">Delete</button></td></tr>';
            });
            if (tb === '') {
                tb = '<tr><td colspan="8"> Not Found </td></tr>';
            }
            $('#tblist').html(tb);
        });
    });

    $(document).on('click', '.sdetail', function (e) {
        var btn = $(this).data('bid');
        $.getJSON('fontend.php', {class: 'userlib', action: 'query', condition: btn}, function (data) {
            $('#bkmod-id').html(data[0].bid);
            $('#bkmod-name').val(data[0].bname);
            $('#bkmod-author').val(data[0].bkauthor);
            $('#bkmod-pub').val(data[0].pubname);
            $('#bkmod-isbn').val(data[0].bkisbn);
            $('#bkmod-desc').val(data[0].bkdesc);
            $('#bkmod-price').val(data[0].bkprice);
            $('#bnmodsave').data('bid',data[0].bid);
            if (data[0].rent == '0') {
                $('#bkmod-rent').text('In Library');
            } else {
                $('#bkmod-rent').text('Rent Out by uid:' + data[0].rent);
            }
            $('#bkmodm').modal('show');
        });
    });

    $('#bncreatebook').click(function () {
        $('#bkmod-id').html('The value is automatically generated');
        $('#bkmod-name').val('');
        $('#bkmod-author').val('');
        $('#bkmod-pub').val('');
        $('#bkmod-isbn').val('');
        $('#bkmod-desc').val('');
        $('#bkmod-price').val('');
        $('#bkmodm').modal('show');
        $('#bkmod-rent').text('In Library');
        $('#bnmodsave').data('bid','');
    });
    
    $('#bnmodsave').click(function(){
        var bid = $(this).data('bid');
        
        if(bid==''){
            $.post('admin.php?class=bookadmin',{action:'add',
            bname: $('#bkmod-name').val(),
            bkauthor: $('#bkmod-author').val(),
            pubname:$('#bkmod-author').val(),
            bkisbn:$('#bkmod-isbn').val(),
            bkprice:$('#bkmod-price').val(),
            bkdesc:$('#bkmod-desc').val()
            },callb,'json');
        }else{
            $.post('admin.php?class=bookadmin',{action:'update',
            bid:bid,
            bname: $('#bkmod-name').val(),
            bkauthor: $('#bkmod-author').val(),
            pubname:$('#bkmod-author').val(),
            bkisbn:$('#bkmod-isbn').val(),
            bkprice:$('#bkmod-price').val(),
            bkdesc:$('#bkmod-desc').val()
            },callb,'json');
        }
        function callb(data){
            if(data.errcode==0){
                $('#msgout').html('<div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> Success, Book Added to the library</div>');
                
            }else{
                $('#msgout').html('<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> '+data.msg+'</div>');
            }
            $('#bkmodm').modal('hide');
        }
    });
    
    $(document).on('click','.bkremove',function(){
        var bid = $(this).data('bid');
        $.getJSON('admin.php?class=bookadmin&action=delete&bid='+bid,function(data){
            if(data.errcode==0){
                $('#msgout').html('<div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> Success, Book is removed from the library</div>');
            }else{
                $('#msgout').html('<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> '+data.msg+'</div>');
            }
            $('#trx'+bid).remove();
        });
    });
});


